<?php

/**
 * 账户资金变动服务
 */

namespace App\Tools;

use Illuminate\Support\Facades\DB;

class Financial
{
    # 账户分类
    public static $accountCate = [1 => '商户', 2 => '用户'];

    # 账户类型
    public static $accountType = [
        1 => '货款', 2 => '管理费', 3 => '运费'
    ];

    # 资金变动方式
    public static $consumeType = [1 => '货款充值', 2 => '管理费充值', 3 => '货款出账', 4 => '管理费出账', 5 => '运费(从货款扣除)', 6 => '运费(从管理费扣除)'];

    /**
     * 获取金额 根据账户信息
     *
     * @param [type] $account_id
     * @param integer $account_type
     * @param integer $type
     * @return void
     * @Author Afree 2020-08-01
     */
    public static function getInfoByAid($account_id, $account_cate = 0, $account_type = 0, $str = '')
    {
        if ($account_id && $account_cate && $account_type) {
            $account = DB::table('account')->where(['account_id' => $account_id, 'account_cate' => $account_cate, 'account_type' => $account_type])->first();
            if ($account) {
                return $str ? $account->$str : (array)$account;
            } else {
                $id = self::addAccount($account_id, $account_cate, $account_type);
                if ($str == 'id') {
                    return $id;
                }
                return $str ? 0 : [];
            }
        }
        return 0;
    }

    /**
     * 扣除费用
     * @param $store_id
     * @param $money
     * @param $expressFee
     * @param $order_id
     */
    public static function changeMoneyByStore($store_id, $money, $expressFee, $order_id)
    {
        $account_id2 = Financial::getInfoByAid($store_id, 1, 2, 'id');
        $storeMoney2 = Financial::getInfoByAid($store_id, 1, 2, 'money');// 管理费

        $account_id1 = Financial::getInfoByAid($store_id, 1, 1, 'id');
        $storeMoney1 = Financial::getInfoByAid($store_id, 1, 1, 'money');// 货款
        $tmpMoney = $storeMoney2 - $money;
        if ($expressFee > 0) {
            if ($tmpMoney >= 0) {
                self::moneyChanges(4, $account_id2, $money, 2, $order_id);
                if ($tmpMoney >= $expressFee) {
                    self::moneyChanges(6, $account_id2, $expressFee, 2, $order_id);
                } else {
                    self::moneyChanges(6, $account_id2, $tmpMoney, 2, $order_id);
                    self::moneyChanges(5, $account_id1, ($expressFee - $tmpMoney), 1, $order_id);
                }
            } else {
                if ($storeMoney2 > 0) {
                    self::moneyChanges(4, $account_id2, $storeMoney2, 2, $order_id);
                    self::moneyChanges(3, $account_id1, ($money - $storeMoney2), 1, $order_id);
                    self::moneyChanges(5, $account_id1, $expressFee, 1, $order_id);
                } else {
                    self::moneyChanges(3, $account_id1, $money, 1, $order_id);
                    self::moneyChanges(5, $account_id1, $expressFee, 1, $order_id);
                }
            }
        } else {
            if ($tmpMoney >= 0) {
                self::moneyChanges(4, $account_id2, $money, 2, $order_id);
            } else {
                if ($storeMoney2 > 0) {
                    self::moneyChanges(4, $account_id2, $storeMoney2, 2, $order_id);
                    self::moneyChanges(3, $account_id1, ($money - $storeMoney2), 1, $order_id);
                } else {
                    self::moneyChanges(3, $account_id1, $money, 1, $order_id);
                }
            }
        }
        return true;
    }

    /**
     * 获取信息
     *
     * @param [type] $id
     * @param string $str
     * @return void
     * @Author Afree 2020-08-03
     */
    public static function getInfoById($id, $str = '')
    {
        if ($id > 0) {
            $account = DB::table('account')->where(['id' => $id])->first();
            if ($account) {
                return $str ? $account->$str : (array)$account;
            }
        }
        return $str ? 0 : [];
    }

    /**
     * 添加账户
     *
     * @param integer $account_id
     * @param integer $account_type
     * @param integer $type
     * @param integer $money
     * @return void
     * @Author Afree 2020-08-03
     */
    public static function addAccount($account_id = 0, $account_cate = 0, $account_type = 0, $money = 0)
    {
        if ($account_id && $account_cate && $account_type) {
            $info = [
                'account_id' => $account_id,
                'account_cate' => $account_cate,
                'account_type' => $account_type,
                'money' => $money
            ];
            return DB::table('account')->insertGetId($info);
        }
        return false;
    }

    /**
     * 账户资金变动表
     */
    public static function moneyChanges($consume_type, $account_id, $money = 0, $to_account_id = 0, $order_id = 0, $to_money = 0)
    {
        if ($money == 0) {
            return false;
        } else {
            $to_money = $to_money > 0 ?: $money;
            # 添加记录
            $account_change_id = self::moneyChangesLog($account_id, $money, $consume_type, $to_account_id, $order_id);
//            v($account_change_id);
            if ($account_change_id) {
                # 减去金额
                DB::table('account')->where(['id' => $account_id])->decrement('money', $money);
                # 添加金额
                DB::table('account')->where(['id' => $to_account_id])->increment('money', $to_money);
                return true;
            }
        }
        return false;
    }

    /**
     * 账户资金变动表日志
     * @param $account_id
     * @param $money
     * @param $name
     * @param $source_id
     * @param $source_order_id
     */
    public static function moneyChangesLog($account_id, $money, $consume_type, $to_account_id, $order_id = 0)
    {
        $money_after = self::getInfoById($account_id, 'money');
        $to_money_after = self::getInfoById($to_account_id, 'money');
        $info = [
            'name' => self::$consumeType[$consume_type],
            'money' => $money,
            'account_id' => $account_id,
            'to_account_id' => $to_account_id,
            'order_id' => $order_id,
            'consume_type' => $consume_type,
            'money_after' => ($money_after - $money),
            'to_money_after' => ($to_money_after + $money),
            'created_at' => time(),
            'updated_at' => time(),
        ];
//        v($info);
        return DB::table('account_money_change')->insertGetId($info);
    }

}
